<template>
  <div class="app-container">
    <div class="customContainer ">
      <!-- 搜索头 -->
      <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
        <dj-store-select :category="642" :defaultStore="formInline.store_id" @change="selectStoreChanged" />
        <el-form-item>
          <el-date-picker style="width:120px" :picker-options="pickerOptions" v-model="pickerDate" value-format="yyyy-MM" type="month" format="yyyy-MM" placeholder="选择月">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <dj-button size="small" @search="searchClick" />
        </el-form-item>
      </el-form>
      <!-- 表格 -->
      <el-table highlight-current-row :data="list" border size="small" v-loading.body="listLoading" :span-method="spanMethod">
        <el-table-column align="center" prop="fir_name" label="项目"></el-table-column>
        <el-table-column align="center" prop="sec_name"> </el-table-column>
        <el-table-column align="center" prop="money" label="金额">
          <template slot-scope="scope">
            <router-link v-if="scope.row.rou_name" :to="{name:scope.row.rou_name,params:{time:searchDate,id:formInline.store_id,routerflag:true}}">
              <el-button size="small" type="text">{{scope.row.money}}</el-button>
            </router-link>
            <span v-else>{{scope.row.money}}</span>
          </template>         
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import { pickerOptionsMonth, getDateString } from '@/utils';
import { money1, money2, money3, money4 } from '@/api/report/giftIOMonthForm';
import { DjStoreSelect } from '@/components/store';
export default {
  components: {
    'dj-store-select': DjStoreSelect
  },
  data() {
    return {
      list: null, 
      listLoading: false,
      searchDate:[],
      formInline: {
        store_id: this.$store.getters.optionId,
        time: '',
        look_del: '0'
      },
      pickerDate: new Date(Date.UTC(new Date().getFullYear(), new Date().getMonth())).toISOString().slice(0, 7),
      pickerOptions:pickerOptionsMonth()
    };
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      //console.log("gift_month_form",1111111)
      if (!vm.list) {
        vm.getListData();
      }
    });
  },
  methods: {
    selectStoreChanged(obj) {
      for (let key in obj) this.formInline[key] = obj[key];
      this.searchClick();
    },
    // 生成列表
    getListData(){
      // this.listLoading = true;
      this.list=[
        {id:0,fir_name:"一、上月库存金额",sec_name:"",money:"0.00",rowspan:1},
        {id:1,fir_name:"二、本月入库金额",sec_name:"供应商采购入库金额",money:"0.00",rowspan:6},
        {id:2,fir_name:"",sec_name:"中央仓采购入库金额",money:"0.00",rowspan:0},
        {id:3,fir_name:"",sec_name:"其他仓调入金额",money:"0.00",rowspan:0,rou_name:"report/stock_allocate_summary_form"},
        {id:4,fir_name:"",sec_name:"本月盘盈金额",money:"0.00",rowspan:0},
        {id:5,fir_name:"",sec_name:"本月库存调整入库金额",money:"0.00",rowspan:0},
        {id:6,fir_name:"",sec_name:"入库合计",money:"0.00",rowspan:0},
        {id:7,fir_name:"三、本月出库金额",sec_name:"礼品机出库金额",money:"0.00",rowspan:17},
        {id:8,fir_name:"",sec_name:"摊位机出库金额",money:"0.00",rowspan:0},
        {id:9,fir_name:"",sec_name:"礼品兑换出库金额",money:"0.00",rowspan:0},
        {id:10,fir_name:"",sec_name:"员工内购出库金额",money:"0.00",rowspan:0},
        {id:11,fir_name:"",sec_name:"企划销售活动出库金额",money:"0.00",rowspan:0},
        {id:12,fir_name:"",sec_name:"企划氛围活动出库金额",money:"0.00",rowspan:0},
        {id:13,fir_name:"",sec_name:"辅助用品出库金额",money:"0.00",rowspan:0},
        {id:14,fir_name:"",sec_name:"办公领用出库金额",money:"0.00",rowspan:0},
        {id:15,fir_name:"",sec_name:"公关领用出库金额",money:"0.00",rowspan:0},
        {id:16,fir_name:"",sec_name:"总部领用出库金额",money:"0.00",rowspan:0},
        {id:17,fir_name:"",sec_name:"生日领用出库金额",money:"0.00",rowspan:0},
        {id:18,fir_name:"",sec_name:"报废出库金额",money:"0.00",rowspan:0},
        {id:19,fir_name:"",sec_name:"本月退货金额",money:"0.00",rowspan:0},
        {id:20,fir_name:"",sec_name:"本月盘亏金额",money:"0.00",rowspan:0},
        {id:21,fir_name:"",sec_name:"调出到其他门店出库金额",money:"0.00",rowspan:0,rou_name:"report/gift_exportStore_summary_form"},
        {id:22,fir_name:"",sec_name:"本月库存调整出库金额",money:"0.00",rowspan:0},
        {id:23,fir_name:"",sec_name:"出库合计",money:"0.00",rowspan:0},
        {id:24,fir_name:"四、库存损益金额",sec_name:"",money:"0.00",rowspan:1},
        {id:25,fir_name:"五、本月库存金额",sec_name:"",money:"0.00",rowspan:1},
      ];
    },
    spanMethod({ row, column, rowIndex, columnIndex }) {
      // console.log(row,"row")
      // console.log(column,"column")
      if (columnIndex === 0) {
          //第一列的合并方法
          const _row_1 = row.rowspan;
          const _col_1 = _row_1 > 0 ? 1 : 0; //如果被合并了_row=0则它这个列需要取消
          return {
            rowspan: _row_1,
            colspan: _col_1
          }
      }
    },
    // 搜索
    searchClick() {
      if(!this.pickerDate){
        this.$message.error("请选择月份");
        return;
      }
      this.formInline.time = this.pickerDate;
      let now_month=new Date(Date.UTC(new Date().getFullYear(), new Date().getMonth())).toISOString().slice(0, 7);
      let start = getDateString('this_month_first_day', this.formInline.time);
      let end = '';
      if(this.formInline.time==now_month){
        end = getDateString('yesterday');
      }else{
        end = getDateString('this_month_last_day', this.formInline.time);
      }
      this.searchDate=[start,end];
      this.fetchData();
    },
    getTotal(){
      this.listLoading=false;
      console.log("total",this.listLoading)
      //入库合计
      this.list[6].money=(this.list[1].money/1+this.list[2].money/1+this.list[3].money/1+this.list[4].money/1+this.list[5].money/1).toFixed(2);
      //出库合计
      this.list[23].money=(this.list[7].money/1+this.list[8].money/1+this.list[9].money/1+this.list[10].money/1+this.list[11].money/1+this.list[12].money/1+this.list[13].money/1+this.list[14].money/1+this.list[15].money/1+this.list[16].money/1+this.list[17].money/1+this.list[18].money/1+this.list[19].money/1+this.list[20].money/1+this.list[21].money/1+this.list[22].money/1).toFixed(2);
    },
    // 列表获取
    fetchData() {
      this.listLoading = true;
      let progress=0;
      let dateData1={
        store_id:this.formInline.store_id,
        look_del:this.formInline.look_del,
        start_time:this.searchDate[0],
        end_time:this.searchDate[1],
      }
      let dateData2={
        store_id:this.formInline.store_id,
        look_del:this.formInline.look_del,
        begin_time:this.searchDate[0],
        end_time:this.searchDate[1],
      }

      money1(this.formInline).then(res => {
        if(res.code==1&&res.data.length){
          let money1Data=res.data[0];
          this.list[0].money=(money1Data.last_money/1).toFixed(2); //上月库存金额
          this.list[4].money=(money1Data.profit/1).toFixed(2); //本月盘盈金额
          this.list[5].money=(money1Data.adjust_in_money/1).toFixed(2); //本月库存调整入库金额
          this.list[19].money=(money1Data.return_money/1).toFixed(2); //本月退货金额
          this.list[20].money=(money1Data.loss/1).toFixed(2); //本月盘亏金额
          this.list[22].money=(money1Data.adjust_out_money/1).toFixed(2); //本月库存调整出库金额
          this.list[25].money=(money1Data.month_money/1).toFixed(2); //本月库存金额
          //库存损益金额
          this.list[24].money=(money1Data.last_money/1 + money1Data.in_money/1 + money1Data.adjust_in_money/1 - money1Data.out_money/1 - 
          money1Data.adjust_out_money/1 + money1Data.profit/1 - money1Data.loss/1 - money1Data.return_money/1 - money1Data.month_money/1).toFixed(2);
        }
        return money2(dateData1);
      }).then(res=>{
        progress++;
        if(res.code==1&&res.message){
          this.list[1].money=(res.message.supplier_money/1).toFixed(2); //供应商采购入库金额
          this.list[2].money=(res.message.house_money/1).toFixed(2); //中央仓采购入库金额
          this.list[3].money=(res.message.other_money/1).toFixed(2); //其他仓调入金额
        }
        if(progress==2){
          this.getTotal();
        }        
      }).catch(res => {
        progress++;
        if(progress==2){
          this.listLoading = false;
        }
      })

      money3(this.formInline).then(res => {
        if(res.code==1){
          if(Object.prototype.toString.call(res.data) === '[object Object]'){
            this.list[7].money=(res.data.gift_out_money/1).toFixed(2); //礼品机出库金额
            this.list[8].money=(res.data.machine_out_money/1).toFixed(2); //摊位机出库金额
            this.list[9].money=(res.data.convertible_out_money/1).toFixed(2); //礼品兑换出库金额
            this.list[10].money=(res.data.worker_out_money/1).toFixed(2); //员工内购出库金额
            this.list[11].money=(res.data.sale_out_money/1).toFixed(2); //企划销售活动出库金额
            this.list[12].money=(res.data.love_out_money/1).toFixed(2); //企划氛围活动出库金额
            this.list[21].money=(res.data.transfer_out_money/1).toFixed(2); //调出到其他门店出库金额
          }
        }
        return money4(dateData2);
      }).then(res=>{
        progress++; 
        if(res.code==1&&res.data.length){
          let list = res.data;
          // 1:报废出库,3:办公领用,4:公关领用,6:总部领用,7:辅助用品,12:生日领用
          this.list[13].money=this.list[14].money=this.list[15].money=this.list[16].money=this.list[17].money=this.list[18].money="0.00";
          for (let key in list) {
            if (list[key].purpose == '1') {
              this.list[18].money = (list[key].out_money/1).toFixed(2); //报废出库金额
            } else if (list[key].purpose == '3') {
              this.list[14].money = (list[key].out_money/1).toFixed(2); //办公领用出库金额
            } else if (list[key].purpose == '4') {
              this.list[15].money = (list[key].out_money/1).toFixed(2); //公关领用出库金额
            } else if (list[key].purpose == '6') {
              this.list[16].money = (list[key].out_money/1).toFixed(2); //总部领用出库金额
            } else if (list[key].purpose == '7') {
              this.list[13].money = (list[key].out_money/1).toFixed(2); //辅助用品出库金额
            } else if (list[key].purpose == '12') {
              this.list[17].money = (list[key].out_money/1).toFixed(2); //生日领用出库金额
            }
          }
        }
        if(progress==2){
          this.getTotal();
        }        
      }).catch(res => {
        progress++;
        if(progress==2){
          this.listLoading = false;
        }
      })
    }
  }
};
</script>